package com.etcn.modules.statical.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etcn.common.config.InterfaceConfig;
import com.etcn.common.service.impl.CrudServiceImpl;
import com.etcn.common.utils.PubFun;
import com.etcn.modules.statical.base.dao.IBaseGoodsDao;
import com.etcn.modules.statical.base.pojo.BaseGoods;
import com.etcn.modules.statical.base.service.IBaseGoodsService;

/**
 * 商品serviceImpl
 * 
 * @author guozc
 * @date 2018-01-12 11:11:28
 */
@Service("baseGoodsService")
public class BaseGoodsServiceImpl extends CrudServiceImpl<IBaseGoodsDao, BaseGoods> implements IBaseGoodsService {
	@Autowired
	private InterfaceConfig interfaceConfig;

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseGoods> findList(BaseGoods entity) throws Exception {
		// 取章
		List<String> sections = new ArrayList<String>();
		String classtype = entity.getClasstype();
		String section = entity.getSection();
		if(StringUtils.isNotEmpty(classtype) && StringUtils.isEmpty(section)){
			List<Map<String, Object>> data = PubFun.requestInterface(
					interfaceConfig.getConfig("query_tariffs"), List.class);
			for(int i=0;i<data.size();i++){
				Map<String, Object> obj = data.get(i);
				if(classtype.equals(obj.get("shortid"))){
					List<Map<String,Object>> clds = (List<Map<String,Object>>)obj.get("children");
					for(int j=0;j<clds.size();j++){
						sections.add((String)clds.get(j).get("id"));
					}
				}
			}
		}else if(StringUtils.isNotEmpty(section)){
			sections.add(section);
		}
		entity.setSections(sections);
		
		List<BaseGoods> goods = super.findList(entity);
		if (goods != null) {
			for (BaseGoods good : goods) {
				if(good.getImportExponent() == null){
					good.setImportExponent("");
				}
				if(good.getExportExponent() == null){
					good.setExportExponent("");
				}
			}
		}
		return goods;
	}

	@Override
	public BaseGoods getByCcode(String hscode) throws Exception {
		return dao.getByCode(hscode);
	}

}